<template>
    <el-card>
        <div class="content">
            <div class="time-info">
                <div class="title">工龄</div>
                <div id="jobAgeEcharts" style="width: 100%; height: 300px"></div>
            </div>
            <div class="area">
                <div class="title">年龄</div>
                <div id="ageEcharts" style="width: 100%; height: 300px"></div>
            </div>
        </div>
        <div class="content">
            <div class="time-info">
                <div class="title">性别</div>
                <div id="genderEcharts" style="width: 100%; height: 300px"></div>
            </div>
            <div class="area">
                <div class="title">婚姻</div>
                <div id="marriageEcharts" style="width: 100%; height: 300px"></div>
            </div>
        </div>
    </el-card>
</template>

<script>
import * as echarts from 'echarts';

export default {
    data() {
        return {}
    },
    mounted() {
        this.postJobAge();
        this.postAge();
        this.postGender();
        this.postMarriage();
    },
    methods: {
        // 统计 - 工龄统计接口----------------------
        postJobAge() {
            this.$http.post(`/employee/employee/statis/jobAge`).then(({data: res}) => {
                // console.log('统计 - 工龄统计接口----------------------', res.data);
                if (res.code !== 0) {
                    return this.$message.error(res.msg);
                }
                this.educationPie(res.data)
            })
        },
        // 统计 - 年龄结构接口-------------
        postAge() {
            this.$http.post(`/employee/employee/statis/age`).then(({data: res}) => {
                // console.log('统计 - 年龄结构接口-------------', res.data);
                if (res.code !== 0) {
                    return this.$message.error(res.msg);
                }
                this.agePie(res.data)
            })
        },
        // 统计 - 性别结构------------------------
        postGender() {
            this.$http.post(`/employee/employee/statis/gender`).then(({data: res}) => {
                // console.log(' 统计 - 性别结构------------------------', res.data);
                if (res.code !== 0) {
                    return this.$message.error(res.msg);
                }
                // 柱状图的数据构造
                let xArr = [], yArr = []
                res.data.forEach(it => {
                    xArr.push(it.name)
                    yArr.push(it.value)
                })
                this.genderColumn(xArr, yArr)
            })
        },
        // 统计 - 婚姻结构接口------------
        postMarriage() {
            this.$http.post(`/employee/employee/statis/marriage`).then(({data: res}) => {
                // console.log('统计 - 婚姻结构接口------------', res.data);
                if (res.code !== 0) {
                    return this.$message.error(res.msg);
                }
                this.marriagePie(res.data)
            })
        },
        educationPie(pieData) {
            var chartDom = document.getElementById('jobAgeEcharts');
            var myChart = echarts.init(chartDom);
            var option;
            option = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a}<br/>{b}:{d}%',
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: '工龄',
                        type: 'pie',
                        radius: '50%',
                        data: pieData,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            option && myChart.setOption(option);
        },
        agePie(pieData) {
            var chartDom = document.getElementById('ageEcharts');
            var myChart = echarts.init(chartDom);
            var option;
            option = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a}<br/>{b}:{d}%',
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: '年龄',
                        type: 'pie',
                        radius: '50%',
                        data: pieData,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            option && myChart.setOption(option);
        },
        genderColumn(xArr, yArr) {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('genderEcharts'));
            // 绘制图表
            myChart.setOption({
                legend: {},
                tooltip: {
                    trigger: 'axis',
                },
                xAxis: {
                    data: xArr,
                },
                yAxis: {
                    name: '年龄'
                },//y轴会自动创建数据
                series: [
                    {
                        name: '性别',
                        type: 'bar',
                        data: yArr,
                    }
                ]
            });
        },
        marriagePie(pieData) {
            var chartDom = document.getElementById('marriageEcharts');
            var myChart = echarts.init(chartDom);
            var option;
            option = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a}<br/>{b}:{d}%',
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: '婚姻',
                        type: 'pie',
                        radius: '50%',
                        data: pieData,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            option && myChart.setOption(option);
        }
    }
}
</script>

<style lang="scss" scoped>
.content {
  display: flex;

  .time-info {
    flex: 1;
  }

  .area {
    flex: 1;
  }
}
</style>